package org.manage.web;

import org.manage.dto.PageBean;
import org.manage.entity.Department;
import org.manage.entity.Employee;
import org.manage.service.DepartmentService;
import org.manage.service.EmployeeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 员工web层
 */

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    /**
     * 1.注册时检查用户名是否重复
     *
     * @return boolean
     */
    @RequestMapping(value = "/check", method = RequestMethod.POST, produces= {"application/json;charset=UTF-8"})
    @ResponseBody
    public boolean checkUsername(@RequestParam("username") String username) {
        logger.debug("username={}" + username);
        // 如果用户名存在
        return employeeService.getUsernameCount(username);
    }

    /**
     * 1.进入首页，检查session
     * 2.如果session存在,直接进入首页,否则执行下一步
     * 3.进入登录页index.jsp
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(HttpSession httpSession) {

        Object sessionName = httpSession.getAttribute("username");
        if (sessionName != null) {
            return "frame";
        }

        return "index";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute Employee employee, HttpSession httpSession) {

        Object sessionName = httpSession.getAttribute("username");
        if (sessionName != null) {
            return "frame";
        }

        int count = employeeService.getByUsernameAndPassword(employee.getUsername(), employee.getPassword());
        if (count == 1) {
            httpSession.setAttribute("username", employee.getUsername());
            httpSession.setMaxInactiveInterval(120);
            return "frame";
        }

        return "index";
    }
    
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpSession httpSession) {
    	httpSession.removeAttribute("username");
    	return "redirect:logout2";
    }
    
    @RequestMapping(value = "/logout2", method = RequestMethod.GET)
    public String logout2() {
    	return "logout";
    }
    
    @RequestMapping(value = "/{currentPage}/{pageSize}/list", method = RequestMethod.GET)
    public String list(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, Model model) {
        PageBean<Employee> pageBean = employeeService.getByPage(currentPage, pageSize);
        model.addAttribute("pageBean", pageBean);
        return "/frame/listEmployee";
    }

//    @RequestMapping(value = "/register", method = RequestMethod.GET)
//    public String register() {
//        return "register";
//    }
//
//    @RequestMapping(value = "/register", method = RequestMethod.GET)
//    public String register(@ModelAttribute Employee e) {
//        if (employeeService.getUsernameCount(e.getUsername())) {
//            employeeService.saveEmployee(e);
//            return "registerSucc";
//        }
//        return "index";
//    }

    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public String addEmployee(Model model) {
        List<Department> list = departmentService.getAllDepartment();
        model.addAttribute("departmentList", list);
        return "/frame/addEmployee";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@ModelAttribute Employee e) {
        if (employeeService.getUsernameCount(e.getUsername())) {
            employeeService.saveEmployee(e);
            return "list";
        }
        return "addError";
    }

    @RequestMapping(value = "/{eid}/update", method = RequestMethod.GET)
    public String edit(@PathVariable("eid") Integer eid, Map<String, Object> model) {
        Employee employee = employeeService.getEmployeeById(eid);
        logger.info("edit employee = " + employee);
        List<Department> departments = departmentService.getAllDepartment();
        logger.info("list department = " + departments);
        model.put("employee", employee);
        model.put("departments", departments);
        return "/frame/editEmployee";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@ModelAttribute Employee employee) {
        if (employeeService.getUsernameCount(employee.getUsername())) {
            employeeService.updateEmployee(employee);
            return "list";
        }
        return "error";
    }

    @RequestMapping(value = "/{eid}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable("eid") Integer eid) {
        employeeService.deleteEmployee(eid);
        return "list";
    }

}
